﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Ms;

namespace VisionCloud.Api.Controllers.Ms
{
    /// <summary>
    /// 任务模块
    /// </summary>
    [SystemModule("任务模块")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/ms/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "任务模块")]
    public class MsTaskController : ApiBaseController
    {
        /// <summary>
        /// 任务服务
        /// </summary>
        public MsTaskService msTaskService { get; set; }

        public HouseMapService houseMapService { get; set; }

        /// <summary>
        /// 获取任务的户型图
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet, SwaggerOperation(Tags = new[] { "任务" })]
        public async Task<ApiResult> GetHouseMapUrl([FromQuery] Guid taskId)
        {
            if (taskId == Guid.Empty)
                throw new OperationException("参数格式不正确");

            return await houseMapService.GetHouseMapBaseUrl(taskId);
        }

        /// <summary>
        /// 获取报告数据处理状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        public async Task<ApiResult> GetReportDataStatus(DeviceReportDataDto request) => await TryCatchAsync(msTaskService.GetReportDataStatus(request));

        /// <summary>
        /// 获取报告数据处理状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取列表数据的字段：Result", typeof(List<DeviceReportDataDto>))]
        public async Task<ApiResult> BatchGetReportDataStatus(List<DeviceReportDataDto> request) => await TryCatchAsync(msTaskService.BatchGetReportDataStatus(request));

        /// <summary>
        /// 根据任务Id获取该任务的汇总和站点报告
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取列表数据的字段：Result", typeof(TaskReportUrlDto))]
        public async Task<ApiResult> GeTaskReportUrls(MsTaskDto dto) => await TryCatchAsync(msTaskService.GeTaskReportUrls(dto));

        /// <summary>
        /// 获取任务详情
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取详情数据的字段：Result", typeof(TaskSfm))]
        public async Task<ApiResult> GetTask(TaskIdWithHouseTypeDto dto) => await TryCatchAsync(msTaskService.GetModel(dto.TaskId, dto.HouseType));

        /// <summary>
        /// 获取任务站点热力图
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取详情数据的字段：Result", typeof(MsPicDto))]
        public async Task<ApiResult> GetMsPicDto(PointDto dto) => await TryCatchAsync(msTaskService.GetMsPicDto(dto));

        /// <summary>
        /// 获取分享的任务站点热力图
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// code = 0 表示获取站点热力图数据成功
        /// code != 0 表示获取数据失败
        /// </remarks>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取详情数据的字段：Result", typeof(MsPicDto))]
        public async Task<ApiResult> GetShareMsPicDto(PointDto dto)
        {
            return await TryCatchAsync(msTaskService.GetMsPicDto(dto, MsTaskService.ShareHotMapDir));
        }

        /// <summary>
        /// 获取单个站点的报告
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// code = 0 表示获取报告成功，result值为url
        /// code != 0 表示暂无报告
        /// </remarks>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取站点报告Url的字段：result", typeof(ApiResult))]
        public async Task<ApiResult> GetSharePointReport(MsTaskPointReportDto dto)
        {
            return await TryCatchAsync(msTaskService.GetPointReportUrl(dto));
        }

        /// <summary>
        /// 任务站点热力图重新绘图
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// Code = Success/0，请求绘图成功，图片链接不变（客户端可能需要注意缓存的问题）
        /// Code = Error/1，请求绘图错误
        /// Code = Fail/2，请求绘图失败
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "Code", typeof(ApiResult))]
        public async Task<ApiResult> RedrawPicture(RedrawDto dto) => await TryCatchAsync(msTaskService.RedrawPicture(dto));

        /// <summary>
        /// 任务站点靠尺图生成
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// Code = Success/0，成功，图片链接不变（客户端可能需要注意缓存的问题）
        /// Code = Error/1，生成靠尺图错误
        /// Code = Fail/2，生成靠尺图失败
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "Code", typeof(ApiResult))]
        public async Task<ApiResult> GenAlgorithmImage(AlgorithmSettingDto dto) => await TryCatchAsync(msTaskService.GenAlgorithmImage(dto));

        /// <summary>
        /// 获取外立面的墙面信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost, SwaggerOperation(Tags = new[] { "任务" })]
        [SwaggerResponse(0, "获取详情数据的字段：Result", typeof(ExternalWallDataDto))]
        public async Task<ApiResult> GetWallDetail(PointDto dto) => await TryCatchAsync(msTaskService.GetExternalWallDataDetail(dto));
    }
}